#!/bin/sh

. $SNAP/utilities/mysql-utilities

root_option_file="$SNAP_DATA/mysql/root.ini"
new_install=false

# Make sure the database is initialized (this is safe to run if already
# initialized)
mysqld --initialize-insecure --basedir="$SNAP" --datadir="$SNAP_DATA/mysql" --lc-messages-dir="$SNAP/share"

# If the above command succeeded, it means this is a new installation.
if [ $? -eq 0 ]; then
	new_install=true
fi

# Start mysql
$SNAP/support-files/mysql.server start

# Initialize new installation if necessary.
if [ $new_install = true ]; then
	# Generate a password for the root mysql user.
	echo -n "Generating root mysql password... "
	root_password=$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c64)
	echo "done."

	# Generate a password for the nextcloud mysql user.
	echo -n "Generating nextcloud mysql password... "
	nextcloud_password=$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c64)
	echo "done."

	# Save root user information
	echo "[client]" >> $root_option_file
	echo "socket=$MYSQL_SOCKET" >> $root_option_file
	echo "user=root" >> $root_option_file
	chmod 600 $root_option_file

	# Now set everything up in one step:
	# 1) Set the root user's password
	# 2) Create the nextcloud user
	# 3) Create the nextcloud database
	# 4) Grant the nextcloud user privileges on the nextcloud database
	echo -n "Setting up users and nextcloud database... "
	mysql --defaults-file=$root_option_file <<SQL
ALTER USER 'root'@'localhost' IDENTIFIED BY '$root_password';
CREATE USER 'nextcloud'@'localhost' IDENTIFIED BY '$nextcloud_password';
CREATE DATABASE nextcloud;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY '$nextcloud_password';
SQL
	if [ $? -eq 0 ]; then
		echo "done."
	else
		echo "Failed to initialize-- reverting..."
		$SNAP/support-files/mysql.server stop
		rm -rf $SNAP_DATA/mysql/*
	fi

	# Now the root mysql user has a password. Save that as well.
	echo "password=$root_password" >> $root_option_file
else
	# Okay, this isn't a new installation. However, we recently changed
	# the location of MySQL's socket (11.0.2snap1). Make sure the root
	# option file is updated to look there instead of the old location.
	sed -ri "s|(socket\s*=\s*)/var/snap/.*mysql.sock|\1$MYSQL_SOCKET|" $root_option_file
fi

# Wait here until mysql is running
wait_for_mysql

# Check and upgrade mysql tables if necessary. This will return 0 if the upgrade
# succeeded, in which case we need to restart mysql.
echo "Checking/upgrading mysql tables if necessary..."
mysql_upgrade --defaults-file=$root_option_file
if [ $? -eq 0 ]; then
	echo "Restarting mysql server after upgrade..."
	$SNAP/support-files/mysql.server restart

	# Wait for server to come back after upgrade
	wait_for_mysql
fi

# If this was a new installation, wait until the server is all up and running
# before saving off the nextcloud user's password. This way the presence of the
# file can be used as a signal that mysql is ready to be used.
if [ $new_install = true ]; then
	mysql_set_nextcloud_password "$nextcloud_password"
fi

# Wait here until mysql exits (turn a forking service into simple). This is
# only needed for Ubuntu Core 15.04, as 16.04 supports forking services.
pid=$(mysql_pid)
while kill -0 "$pid" 2>/dev/null; do
	sleep 1
done
